﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.exceptions;
using com.eforceglobal.crux.security;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class ManageDepartmentsController
    {
        IManageDepartments _view = null;
        public ManageDepartmentsController(IManageDepartments view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
            {
                CheckValidity();
                SwitchToListMode();
            }
        }

        private void CheckValidity()
        {
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            SessionData sessionData = new SessionData();
            UserInformation currentuser = (UserInformation)sessionData.CurrentUser;
            if (!cruxSecurityManager.IsUserSuperAdminOrCompanyAdmin(currentuser, _view.CompanyID))
                NavigationController.OpenErrorPage("803");

        }
        
        public void OpenDepartment(int DepartmentID)
        {
            DepartmentFacade departmentFacade = new DepartmentFacade();
            DepartmentInformation departmentInformation = new DepartmentInformation();
            departmentInformation = departmentFacade.GetDepartment(DepartmentID);
            _view.CurrentDepartment = departmentInformation;
            _view.FormMode = FormMode.AddEdit;
        }

        public CompanyInformation GetCompanyForDepartment(int CompanyID)
        {
            ICruxSecurityManager securityManager = CruxSecurityManagerFactory.GetSecurityManager();
            return securityManager.GetCompany(CompanyID);
        }


        public void CreateDepartment()
        {
            OpenDepartment(0);
        }


        public void SaveDepartment()
        {
            try
            {
                DepartmentFacade departmentFacade = new DepartmentFacade();
                departmentFacade.SaveDepartment(_view.CurrentDepartment);
                SwitchToListMode();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Description);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }

        }

        public void SwitchToListMode()
        {
            _view.ErrorDetails = null;
            _view.CurrentDepartment = new DepartmentInformation();
            DepartmentFacade departmentFacade = new DepartmentFacade();
            _view.Departments = departmentFacade.GetDepartments(_view.CompanyID);
            _view.FormMode = FormMode.List;
        }

        public void CancelAddEdit()
        {
            SwitchToListMode();
        }

        public void DeleteDepartment(int DepartmentID)
        {

            try
            {
                DepartmentFacade departmentFacade = new DepartmentFacade();
                departmentFacade.DeleteDepartment(DepartmentID);
                SwitchToListMode();
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
        }

        public void RedirectToPreviousPage()
        {
            NavigationController.OpenView("ManageCompany", _view.CompanyID);
        }
    }
}
